import Flutter
import Contacts

public class IosContactPermissionPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "ios_contact_permission", binaryMessenger: registrar.messenger())
    let instance = IosContactPermissionPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    switch call.method {
    case "getContactPermission":
      result(CNContactStore.authorizationStatus(for: .contacts).rawValue)
    case "requestContactPermission":
    let contactStore = CNContactStore()
        contactStore.requestAccess(for: .contacts) { (granted, error) in
            result(granted)
        }
    default:
      result(FlutterMethodNotImplemented)
    }
  }
}
